﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VNet.VisiSuite.Service.Contact.Message.Security;
using VNet.VisiSuite.VisiAdmin.Controls;
using VNet.VisiSuite.VisiAdmin.ViewModels;
using VNet.VisiSuite.VisiAdmin.Views;

namespace VNet.VisiSuite.VisiAdmin.Common.Helpers
{
    public static class NavigationManager
    {
        #region Fields
        private static bool _wflag = false;
        private static VisiPage _currentPage = null;
        private static ContentControl _inactiveContent = null;
        private static ContentControl _activeContent = null;
        private static DoubleAnimation _animation = null;
        private static MainWindow _mainWindow;
        #endregion

        public static SignInResponse LoggedInUser { get; set; }

        public static void SetMainWindow(MainWindow window)
        {
            _mainWindow = window;

            if (_mainWindow == null) return;

            _mainWindow.MouseRightButtonDown += _mainWindow_MouseRightButtonDown;
            _mainWindow.MouseLeftButtonDown += _mainWindow_MouseLeftButtonDown;
            _mainWindow.KeyDown += _mainWindow_KeyDown;

            _activeContent = _mainWindow._content1;
            _inactiveContent = _mainWindow._content2;

            _mainWindow._topPanel.OnCloseCallback(HidePanel);
            _mainWindow._bottomPanel.OnCloseCallback(HidePanel);

            NavigateTo(new LoginView(), null);
        }

        private static void _mainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F11)
            {
                switch (_wflag)
                {
                    case false:
                        _mainWindow.WindowState = WindowState.Normal;
                        _mainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                        _mainWindow.Topmost = false;
                        _mainWindow.ResizeMode = ResizeMode.CanResize;
                        _wflag = true;
                        break;
                    case true:
                        _mainWindow.Visibility = Visibility.Collapsed;
                        _mainWindow.WindowState = WindowState.Maximized;
                        _mainWindow.WindowStyle = WindowStyle.None;
                        _mainWindow.Topmost = true;
                        _mainWindow.ResizeMode = ResizeMode.NoResize;
                        _mainWindow.Visibility = Visibility.Visible;
                        _wflag = false;
                        break;
                }
            }
            else if (_currentPage != null)
            {
                _currentPage.DoKeyDown(e);
            }
        }

        private static void _mainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            double yy = e.GetPosition(_mainWindow).Y;

            if (!_mainWindow._topPanel.Contains(yy) && !_mainWindow._bottomPanel.Contains(_mainWindow._main_Grid.RenderSize.Height, yy))
                HidePanel();
        }

        private static void _mainWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            if (!_mainWindow._topPanel.IsHidden() || !_mainWindow._bottomPanel.IsHidden())
            {
                _mainWindow._topPanel.Hide();
                _mainWindow._bottomPanel.Hide();
            }
            else
            {
                _mainWindow._topPanel.Show();
                _mainWindow._bottomPanel.Show();
            }
        }

        public static void HidePanel()
        {
            if (_mainWindow == null) return;

            _mainWindow._topPanel.Hide();
            _mainWindow._bottomPanel.Hide();
        }

        public static void ShowPanel(PanelType panelType)
        {
            if (_mainWindow == null) return;

            if (panelType == PanelType.Top || panelType == PanelType.Both)
                _mainWindow._topPanel.Show();
            if (panelType == PanelType.Bottom || panelType == PanelType.Both)
                _mainWindow._bottomPanel.Show();
        }

        public static void NavigateTo(VisiPage page, object navigationObj)
        {
            if (_mainWindow == null) return;

            HidePanel();

            if (page != null && page != _currentPage)
            {
                //Invoke OnLeave method to clear the previous View
                if (_currentPage != null)
                {
                    var oldViewModel = _currentPage.DataContext as BaseViewModel;
                    if (oldViewModel != null)
                        oldViewModel.OnLeave();
                }

                // clear out old menu items
                _mainWindow._topPanel.SetTiles(string.Empty, null);
                _mainWindow._bottomPanel.SetIcons(null);
                _mainWindow._bottomPanel.UserControl(null);
                _activeContent.IsEnabled = false;

                _inactiveContent.SetValue(Panel.ZIndexProperty, 0);
                _inactiveContent.Content = page;

                TranslateTransform transform;
                if (_inactiveContent == _mainWindow._content1)
                    transform = _mainWindow._content1Translate;
                else
                    transform = _mainWindow._content2Translate;

                transform.X = _activeContent.ActualWidth;
                _inactiveContent.IsEnabled = true;
                //_inactiveContent.Visibility = Visibility.Visible;

                _inactiveContent.SetValue(Panel.ZIndexProperty, 100);

                var duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
                _animation = new DoubleAnimation(_activeContent.ActualWidth, 0, duration);
                _animation.Completed += delegate(object sender, EventArgs e)
                {
                    _animation.FillBehavior = FillBehavior.Stop;

                    //_activeContent.Visibility = Visibility.Hidden;
                    _activeContent.SetValue(Panel.ZIndexProperty, 0);
                    transform.X = _activeContent.ActualWidth;
                    _activeContent.Content = null;

                    ContentControl swap = _activeContent;
                    _activeContent = _inactiveContent;
                    _inactiveContent = swap;

                    //// add new menus
                    _mainWindow._topPanel.SetTiles(_currentPage.TopMenuDescription(), _currentPage.TopMenuItems());
                    _mainWindow._bottomPanel.SetIcons(_currentPage.BottomMenuItems());
                    _mainWindow._bottomPanel.UserControl(_currentPage.BottomMenuControl());

                    _activeContent.SetValue(Panel.ZIndexProperty, 50);

                    //Invoke OnNavigate method to get data on next View
                    var newViewModel = _currentPage.DataContext as BaseViewModel;
                    if (newViewModel != null)
                        newViewModel.OnNavigate(navigationObj);
                };

                transform.BeginAnimation(TranslateTransform.XProperty, _animation);
                _currentPage = page;
            }
        }

        public static void CloseProgram()
        {
            if (_mainWindow != null)
                _mainWindow.Close();
        }
    }

    public enum PanelType
    {
        Top,
        Bottom,
        Both
    }
}